﻿#pragma once

#include "Math/Color.h"
#include "Math/Rect.h"
#include "Misc/CommonMacros.h"
#include "Render/PreRender/PreRenderWidgets.h"
#include <vulkan/vulkan.hpp>
#include "Render/Scene/CameraParameters.h"
#include "Render/AntiAlisingMethod.h"
#include "Render/Widget/ImageWidgetScaleMethod.h"
#include "Render/Scene/RenderScene.h"
#include "Font/CharTexture.h"

class RTexture2D;
class CRenderSurface;
class CRenderScene;
class SRenderForegroundMeshInstance;
class CAllPreRenderData;
struct SPreRenderContext;
class CRenderPixelObjectInstanceGroup;

struct WHENGINE_API SRenderWidgetBase
{

    //越小越靠后
    uint16_t ZOrder=0;
    float ToDepth()
    {
        assert(ZOrder<=std::numeric_limits<uint16_t>::max()
        && "Zorder not in a proper range , cant paint" );
        //convert from [0,65535] to (1,0)
        return (std::numeric_limits<uint16_t>::max()-(ZOrder+1 
            /* +1 is to avoid ZOrder become 1
            ,it is hard to say the widget with ZOrder=1 will be rendered or not*/

        ))/(float)std::numeric_limits<uint16_t>::max();
    }


    //widget position and size in pixel
    SRect Rect; 

    //clip rect in pixel
    SRect ClipRect;

    //生成更底层的PreRenderWidget
    virtual void GeneratePreRenderWidgets(
        SPreRenderContext& InPreRenderContext,
        CRenderSurface *InRenderSurface,
        SPreRenderWidgets &OutPreRenderWidgets
    )=0;
};


//描述一个矩形控件的所有数据
struct WHENGINE_API SRenderRectWidget:public SRenderWidgetBase
{
    bool bFill=false;
    SColor FillColor;

    float BorderSize=0.0f;
    SColor BorderColor;
    
    float Radius=0.0f;
    
    virtual void GeneratePreRenderWidgets(
        SPreRenderContext& InPreRenderContext,
        CRenderSurface *InRenderSurface,
        SPreRenderWidgets &OutPreRenderWidgets
    ) override;

};

struct WHENGINE_API SRenderLineWidget:public SRenderWidgetBase
{
    SColor Color;
    SVec2 Start;
    SVec2 End;
    float Width=1.0f;

    virtual void GeneratePreRenderWidgets(
        SPreRenderContext& InPreRenderContext,
        CRenderSurface *InRenderSurface,
        SPreRenderWidgets &OutPreRenderWidgets
    ) override;


};


struct WHENGINE_API SRenderImageWidget:public SRenderWidgetBase
{
    //图片资源
    std::shared_ptr<CRenderTexture2D> Image;

    //缩放方式
    EImageWidgetScaleMethod ScaleMethod=EImageWidgetScaleMethod::Fill;

    SColor TintColor=SColor(1,1,1,1);

    //自定义的左上角和右下角UV
    //仅在缩放方式为Custom时有效
    SVec2 CustomLeftTopUV;
    SVec2 CustomRightBottomUV;

    virtual void GeneratePreRenderWidgets(
        SPreRenderContext& InPreRenderContext,
        CRenderSurface *InRenderSurface,
        SPreRenderWidgets &OutPreRenderWidgets
    ) override;
};

//单个三角形控件
struct WHENGINE_API SRenderTriangleWidget: public SRenderWidgetBase
{
    SColor Color;

    //在控件上顺时针的三个点
    SVec2 P0;
    SVec2 P1;
    SVec2 P2;

    virtual void GeneratePreRenderWidgets(
        SPreRenderContext& InPreRenderContext,
        CRenderSurface *InRenderSurface,
        SPreRenderWidgets &OutPreRenderWidgets
    ) override;
};


//单个圆控件
struct WHENGINE_API SRenderCircleWidget:public SRenderWidgetBase
{
    SColor Color;

    //圆心,单位是像素,与rect的左上角相对位置
    SVec2 Center;

    //半径,单位是像素
    float Radius;

    virtual void GeneratePreRenderWidgets(
        SPreRenderContext& InPreRenderContext,
        CRenderSurface *InRenderSurface,
        SPreRenderWidgets &OutPreRenderWidgets
    ) override;
};




struct WHENGINE_API SRenderDynamicTextWidget: public SRenderWidgetBase
{
public:
    //文字图片资源
    TVector<SRenderCharTexture> CharTextures;

    //文字图片在视口中的位置,单位是像素
    TVector<SVec2> CharPositions;

    //文字图片的颜色
    TVector<SColor> CharColors;

    virtual void GeneratePreRenderWidgets(
        SPreRenderContext& InPreRenderContext,
        CRenderSurface *InRenderSurface,
        SPreRenderWidgets &OutPreRenderWidgets
    ) override;
};

//渲染富文本，由主线程将文本合成一张大图，然后传给预渲染线程
// struct WHENGINE_API SRenderTextWidget:public SRenderWidgetBase
// {
//     //文字图片资源
//     std::shared_ptr<CRenderTexture2D> TextImage;

//     //文字在图片中的位置,单位是像素
//     TVector<SVec2> CharPositions;

//     //文字在图片中的大小,单位是像素
//     TVector<SVec2> CharSizes;

//     //文字图片的颜色
//     TVector<SColor> CharColors;

//     virtual void GeneratePreRenderWidgets(
//         SPreRenderContext& InPreRenderContext,
//         CRenderSurface *InRenderSurface,
//         SPreRenderWidgets &OutPreRenderWidgets
//     ) override;
// };

struct WHENGINE_API SRenderStaticTextWidget :public SRenderWidgetBase
{
    //文字图片资源
    TVector<SRenderCharTexture> CharTextures;

    //文字图片在视口中的位置,单位是像素
    TVector<SVec2> CharPositions;

    //文字图片的颜色
    SColor TextColor;


    virtual void GeneratePreRenderWidgets(
        SPreRenderContext& InPreRenderContext,
        CRenderSurface *InRenderSurface,
        SPreRenderWidgets &OutPreRenderWidgets
    ) override;

};

struct WHENGINE_API SRenderViewportWidget : public SRenderWidgetBase
{
public:
    TVector<std::shared_ptr<CRenderScene>> Scenes;
    
    //前景色，用于绘制坐标轴
    TVector<std::shared_ptr<SRenderForegroundMeshInstance>> ForegroundMeshInstances;

    //相机参数
    SRenderCameraParameters CameraParameters;
    
    //抗锯齿
    EAntiAliasingMethod AntiAliasingMethod=EAntiAliasingMethod::None;

    //像素对象映射,如果没有启用，这个对象为空
    std::shared_ptr<CRenderPixelObjectInstanceGroup> PixelObjects;
    

    virtual void GeneratePreRenderWidgets(
        SPreRenderContext& InPreRenderContext,
        CRenderSurface *InRenderSurface,
        SPreRenderWidgets &OutPreRenderWidgets
    ) override;

};